<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="java.text.NumberFormat"%>
<%@page import="com.is.control.AttendanceTimeControl"%>
<%@page import="com.is.control.AttendanceDetailControl"%>
<%@page import="com.is.model.Student"%>
<%@page import="com.is.control.StudentControl"%>
<%@page import="com.is.util.ParamUtil"%>
<%@page import="com.is.control.ParamCourseControl"%>
<%@page import="com.is.control.ParamClassControl"%>
<%@page import="com.is.control.ParamStuyearControl"%>
<%@page import="com.is.control.CourseClassControl"%>
<%@page import="com.is.model.CourseClass"%>
<%@page import="com.is.model.User"%>
<%@page import="com.is.util.DateTime"%>
<%@page import="com.is.model.ParamCourse"%>
<%@page import="com.is.model.ParamClass"%>
<%@page import="com.is.util.DesData"%>
<%@page import="com.is.model.ParamReasonType"%>
<%@page import="com.is.control.ParamReasonTypeControl"%>
<%@page import="com.is.model.ParamReasonTypeGS"%>
<%@page import="com.is.control.ParamReasonTypeGSControl"%>
<%@page import="com.is.model.ParamStuyear"%>
<%@page import="com.is.export.model.AttendanceExport"%>
<%@page import="com.is.export.control.SelectOptionValue"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="/InterStu/resources/image/favicon.ico" />
<link rel="stylesheet" type="text/css" href="../../resources/css/all.css" />
<link href="../../resources/css/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../resources/js/jquery-1.8.1.js"></script>
<script type="text/javascript" src="../../resources/js/jquery.artDialog.js"></script>
<script type="text/javascript" src="../../resources/js/WebCalendar.js"></script>
<script type="text/javascript" src="../../resources/js/iframeTools.js"></script>
<script type="text/javascript">
	(function (config) {
	    config['fixed'] = true;
	    config['okVal'] = '确定';
	    config['cancelVal'] = '取消';
		config['skin'] = 'default';
		config['drag'] = 'true';
	    // [more..]
	})(art.dialog.defaults);	
	$(function(){
		$(".update").click(function(){
			$.dialog.open("/InterStu/update_info.jsp",{width:460,height:400,title:"个人信息"});
		});
		$('img').error(function(){
			this.src = "/InterStu/resources/image/man.GIF";
		});
		var block = $("#magic_block");
		if($(".active a").position()){
			block.css("top",$(".active a").position().top+10)
			.data("oriTop",$(".active a").position().top);
		}
		$(".nav ul li").find("a").hover(
			function(){
				var $this = $(this);
				var newTop = $this.position().top;
				block.stop().animate({
					top:newTop+10
				});
			},
			function(){
				block.stop().animate({
					top:block.data("oriTop")+10
				});
			}
		);
		var h = $(".right").height();
		$(".left").height(h);
		$(".team").click(function(){
			var param = $(this).attr("rel");
			$.dialog.open("transform.jsp?usingStuyear="+param,{width:280,height:120,title:"折算公式"});
		});
		$(".allCourse").click(function(){
			var url = $(this).attr("rel");
			$.dialog.open(url,{width:460,cancel:true,cancelVal:"返回",height:'auto'});
		});
		$("[name='stuyear']").change(function(){
			var selectYear = $(this).val();
			$.ajax({
				type:"post",
				dataType:"html",
				url:"../homework/select_option.jsp",
				data:{
					stuYearId:selectYear
				},
				cache:false,
				success:function(data){
					$("[name='courseClassId']")[0].options.length = 0;
					$("[name='courseClassId']").append(data);
				}
			});
		});
	});
	function doSubmit(){
      var submit = document.getElementById("submit");
      submit.disabled="disabled";
      return true;
	}
	
</script>
<style type="text/css">
	.team{text-shadow:none;}
</style>
<title>留学生预科班</title>
</head>
<body>
<%
User cu = (User)session.getAttribute("user");
%>
<div class="header">
	<div class="title pr">
    	<a href="javascript:void(0);"><span class="update pa">个人信息</span></a>
        <span class="logout pa"><a class="hide" href="/InterStu/login/logout.jsp">退出</a></span>
    </div>
</div>
<div class="main">
	<div class="left">  
		<div class="infomation">
			<div class="photo">
			<%
			String picUrl = cu.getPicUrl();
			%>
				<img width='60' height="60" src = "<%=picUrl %>"  />
			</div>
			<div class="welcome">
				<%=cu.getName() %>,<br/> 
				欢迎您使用！s</div>
		</div>  	
    	<div class="nav">
        	<ul>	
        		<li class="pa" id="magic_block"></li>
            	<%if(cu!=null&&cu.getRole().equals("1")){ %>
                <li><a href="/InterStu/pages/stuyear/stuyear_start.jsp">新学期设置</a></li>
                <li><a href="/InterStu/pages/users/user_all.jsp">用户管理</a></li>
                <%} %>
                <li class="active"><a href="/InterStu/pages/students/student_add.jsp">学生管理</a></li>
                <li><a href="/InterStu/pages/dictation/dictation_add.jsp">学生考核</a></li>
                <%if(cu!=null&&cu.getRole().equals("1")){ %>
                <li><a href="/InterStu/pages/assess/teacher_assess.jsp">教职人员管理</a></li>
                <%} %>
            </ul>
        </div>
    </div>
    <div class="right">
    	<div class='r1'>
    	<table class="student tab">
    <tr>
    <%if(cu!=null&&cu.getRole().equals("1")){ %>
    	<td>
    		<a href="/InterStu/pages/students/student_add.jsp">添加学生</a>
		</td>
		<%} %>
		<td>
			<a href="/InterStu/pages/students/student_admin.jsp">查看学生</a>
		</td>
		<td>
			<a class='current' href="/InterStu/pages/attendance/attendance_add.jsp">学生出勤</a>
		</td>
    </tr>
    </table>
    	<%
	   //这个页面需要获取
	   List<CourseClass> ccs = null;
	   String time1 = DateTime.SimpleDate();
	   String time2 = DateTime.SimpleDate();
	   int stuyear = ParamStuyearControl.getCurrentStuyearId();
	   int usingStuyear = ParamUtil.getInt(request,"stuyear",stuyear);
	   session.setAttribute("stuYearId",usingStuyear);
	   ParamStuyear usingPS = ParamStuyearControl.getStuyear(usingStuyear);
	   List<AttendanceExport> exportList = new ArrayList<AttendanceExport>(); //导出的list
	   if(usingStuyear!=0&&usingPS!=null){
	        // System.out.print(usingPS.getStuyear());System.out.print(usingPS.getStartTime());
	         time1 = usingPS.getStartTime();//默认为本学期开学时间
	     }else{
		    time1 = ParamUtil.getString(request,"time1",usingPS.getStartTime());
	     }
	   if(stuyear==usingStuyear){
	        time2 = ParamUtil.getString(request,"time2",DateTime.SimpleDate());
	   }else{
		    time2 = usingPS.getEndTime();
	   }
	   if(ParamUtil.getInt(request, "type",0)==1){
		   time1 = ParamUtil.getString(request,"time1",time1);
		   time2 = ParamUtil.getString(request,"time2",time2);
	   }
	   if(DateTime.dateToInt(usingPS.getStartTime())>DateTime.dateToInt(time1)||(DateTime.dateToInt(usingPS.getEndTime())<DateTime.dateToInt(time2))
	   ||DateTime.dateToInt(time1)>DateTime.dateToInt(time2)){
	   		%>
	   		 <script>
 			   $.dialog({
   				    title: '查询结果',
   				    content: '查询时间不在选定学期范围内(正确范围：<%=usingPS.getStartTime()%>~<%=usingPS.getEndTime()%>)',
   				    icon: 'warning',
   				    ok: function(){
   				         window.location.href='attendance_statistics.jsp?courseClassId=0&stuyear=<%=usingPS.getId()%>';
   				    }
   				});
	   		</script>
	   		<%
	   	
	   }
	   int stuYear = (Integer)session.getAttribute("stuYearId");
	   
	   int courseClassId = ParamUtil.getInt(request,"courseClassId",0);
	   if(cu!=null&&cu.getRole().equals("1")){
		   //管理员应该能查看所有的课程班级出勤情况
		  ccs = CourseClassControl.getAllCourseClassIdByUidAndStuyear(0,stuYear);
	   }
	   if(cu!=null&&cu.getRole().equals("0")){
		   //任课教师看到自己所带课程的出勤情况
		   ccs = CourseClassControl.getAllCourseClassIdByUidAndStuyear(cu.getId(),stuYear);
	   }
	   if(ccs==null||ccs.size()==0){
		   out.print("本学期尚未有任何课程教学工作。");
	   }else{
	   %>
	   <form action="attendance_statistics.jsp?type=1" method="post">
	   <table class='table'>
	   <caption>请选择<%if(cu.getRole().equals("1")){ %><a href="oneClassAllAttendance.jsp">【查询班级出勤详细】</a><%} %></caption>
	   <tr>
	   <td>学期</td>
	   <td><select name="stuyear" id="stuyear">
	   <%
	    List<ParamStuyear> stuyears = ParamStuyearControl.getAllStuyear();
	     for(ParamStuyear ps:stuyears){
	    	 %>
	    	 <option value="<%=ps.getId() %>"  <%if(ps.getId()==usingStuyear)out.print("selected"); %>><%=ps.getStuyear() %></option>
	    	 <%
	     }
	   %>
	   </select></td>
	   <td>
	   		班级&课程
	   </td>
	   <td>
	   		<select name="courseClassId">
			   <%
			      for(CourseClass cc:ccs){
			   %>
			   <option value="<%=cc.getId() %>" <%if(cc.getId()==courseClassId) out.print("selected='selected'"); %>><%=ParamClassControl.getClassName(cc.getClassId()) %><%=ParamCourseControl.getCourseById(cc.getCourseId()).getCourseName() %></option>
			   <%} %>
		   </select>
	   </td>
	   <td>开始时间</td>
	   <td><input id="time1" type='text'  name="time1" onclick="SelectDate(this,'','','yyyy-MM-dd')"  readonly style="width:80px;cursor:pointer"  value="<%=time1 %>"/></td>
       <td>结束时间</td>
	   <td><input id="time2" type='text'  name="time2" onclick="SelectDate(this,'','','yyyy-MM-dd')"  readonly style="width:80px;cursor:pointer"  value="<%=time2 %>"/></td>
       <td><input type="submit" value="查询"/></td>
       </tr>
       </table>
	   </form>
	   <%
   }
   
   %>
   </div>
   <div class="r2">
   <%
        if(courseClassId>0){
        	CourseClass nowcc = CourseClassControl.getCourseClassById(courseClassId);
        	String courseName = ParamCourseControl.getCourseById(nowcc.getCourseId()).getCourseName();
        	String className = ParamClassControl.getClassName(nowcc.getClassId());
        	List<Student> stus = StudentControl.getStudentByClassId(String.valueOf(nowcc.getClassId()));
        	DesData des = new DesData();
        	int countTimes = AttendanceTimeControl.getCount(nowcc.getId(),usingStuyear,time1,time2);
        	%>
        	<form>
        	<table class="table">
        	<caption>
        		<%=className %><%=courseName %>此期间内该课程已点名<%=countTimes%>次
        		<a href="/InterStu/servlet/ExportDo" target="_blank">【导出查询结果】</a><a href="attendanceTimeList.jsp?courseClassId=<%=courseClassId%>">【查看单次出勤情况】</a>
        	</caption>
        	<thead>
        	<tr>
        	<th>姓名</th>
        	<th>迟到</th>
        	<th>缺勤</th>
        	<th>病假</th>
        	<th>事假</th>
        	<th><a href="javascript:void(0);" rel="<%=usingStuyear %>" class="team">折为总缺勤数（/次）</a></th>
        	<th>缺勤率</th>
        	</tr>
        	</thead>
        	<tbody>
        	<%
        	  for(Student stu:stus){
        		  AttendanceExport ae = new AttendanceExport();
        		  %>
        		  <tr>
        		   <td><a href="javascript:void(0);" class="allCourse" rel="allCoursesOneStu.jsp?stuId=<%=des.encrypt(String.valueOf(stu.getId())) %>"><%=stu.getChineseName() %></a></td>
        		  <%
        		  float totalQQ=0;
        		  int totalDM = AttendanceTimeControl.getCount(nowcc.getId(),usingStuyear,"","");
        		  boolean flag = true;
		          int count2 = AttendanceDetailControl.getCount(2,stu.getId(),nowcc.getId()+"",usingStuyear,time1,time2);
		          int num2 = ParamReasonTypeGSControl.getNumByTypeAndStuyear(2,usingStuyear);
		          if(num2==0){
		        	  flag = false;
		          }else{
		        	  totalQQ+= (float)count2/num2;
		          }
		          int count3 = AttendanceDetailControl.getCount(3,stu.getId(),nowcc.getId()+"",usingStuyear,time1,time2);
		          int num3 = ParamReasonTypeGSControl.getNumByTypeAndStuyear(3,usingStuyear);
		          if(num3==0){
		        	  flag = false;
		          }else{
		        	  totalQQ+= (float)count3/num3;
		          }
		          int count4 = AttendanceDetailControl.getCount(4,stu.getId(),nowcc.getId()+"",usingStuyear,time1,time2);
		          int num4 = ParamReasonTypeGSControl.getNumByTypeAndStuyear(4,usingStuyear);
		          if(num4==0){
		        	  flag = false;
		          }else{
		        	  totalQQ+= (float)count4/num4;
		          }
		          int count5 = AttendanceDetailControl.getCount(5,stu.getId(),nowcc.getId()+"",usingStuyear,time1,time2);
		          int num5 = ParamReasonTypeGSControl.getNumByTypeAndStuyear(5,usingStuyear);
		          if(num5==0){
		        	  flag = false;
		          }else{
		        	  totalQQ+= (float)count5/num5;
		          }
		          float absenceRate = 0;
		          String absencePer="";
		          if(countTimes == 0){
		          	  //flag = false;
		          	   absenceRate = 0;
		          	   absencePer = "0";
		          } else{
		        	  NumberFormat nt = NumberFormat.getPercentInstance();
		        	  nt.setMinimumFractionDigits(2);
		          	  absenceRate = totalQQ/countTimes;
		          	  absencePer =nt.format(absenceRate);
		          }
		          exportList.add(new AttendanceExport(stu.getChineseName(),courseName,count2+"",count3+"",count4+"",count5+"",totalDM+"",totalQQ+"",absencePer));
				      %>
				  <td><%=flag?count2:"公式有错误或者尚未设置，请联系管理员"%></td>
				  <td><%=flag?count3:"公式有错误或者尚未设置，请联系管理员"%></td>
				  <td><%=flag?count4:"公式有错误或者尚未设置，请联系管理员"%></td>
				  <td><%=flag?count5:"公式有错误或者尚未设置，请联系管理员"%></td>
				  <td><%=flag?totalQQ:"公式有错误或者尚未设置，请联系管理员"%></td>
				  <td><%=flag?absencePer:"公式有错误或者尚未设置，请联系管理员"%></td>
		        		  <%
		        		  }
        	 session.removeAttribute("exportList");
        	 session.setAttribute("exportList",exportList);
        	 session.removeAttribute("fileName");
        	 session.setAttribute("fileName",className+courseName+"出勤统计");
        	 LinkedHashMap<String,String> exportOps = SelectOptionValue.AttendanceOptions();
        	 session.removeAttribute("exportOps");
        	 session.setAttribute("exportOps",exportOps);
        	 session.removeAttribute("className");
        	 session.setAttribute("className","com.is.export.model.AttendanceExport");
		        		  %>
   		   </tr>
        	</tbody>
        	</table>
        	</form>
        	<%
        }
   %>
    </div>
    </div>
</div>
<div class="footer">
	<p>版权所有：东北师范大学留学生预科部&nbsp;&nbsp;  蓝旭工作室开发</p>
</div>
</body>
</html>